import {defineStore} from 'pinia'
import store from 'store2'
import _ from 'lodash'
import {inject} from "vue";
const useWebsiteStore = defineStore('websiteStore',{
    state(){
        return{
            websites:store.get("websites") || []
        }
    },
    actions:{
        add(item){
            if(_.find(this.websites,{url:item.url})){
                myApi.alert("此网站已经被添加")
                return
            }
            this.websites.unshift(item)
            store('websites',this.websites)
        },
        delItem(url){
            this.websites = this.websites.filter(item=>{
                return item.url !== url
            })
            store('websites',this.websites)
        },
    },
    getters:{
        find(){
            return (inputs)=>{
                if(inputs === ""){
                    return this.websites
                }else{
                    return _.filter(this.websites,(item)=>{
                        let partten = new RegExp(inputs,'i')
                        return partten.test(item.title)
                    })
                }
            }
        }
    }
})

export  {useWebsiteStore}